﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UnityScriptEngine;

namespace MBBHotelReservation
{
    public partial class frmLogin : Form
    {
        UnityProcessor proc = new UnityProcessor();
        UnityScript[] dat = new UnityScript[0];
        Threader t = new Threader();
        UnityElement user;
        string PWD = "";
        public frmLogin()
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            InitializeComponent();
            GUIEvents();
            UnityEvents();
        }
        void GUIEvents()
        {
            txtUID.LostFocus += new EventHandler(delegate
            {
                LogState(false);
                txtNotice.Text  = "";
                UnityScript u = new UnityScript("CHECKUSERDATA", 5);
                u.Script_Add(Script.DBPUX_BEGIN);
                u.Script_Add(Script.DBPUX_QUERY, string.Format("SELECT fID, fUID, fName, fPWD, fLevel, fStamp FROM tblusers WHERE " +
                    "fUID='{0}' OR fName='{0}'",
                    txtUID.Text)
                    );
                u.Script_Add(Script.DBPUX_COMMIT);
                proc.Run(u);
            });
            txtPWD.KeyUp += new KeyEventHandler(delegate(object sender, KeyEventArgs e)
            {
                if (txtPWD.Text.Length == PWD.Length)
                {
                    if (txtPWD.Text != PWD)
                    {
                        txtNotice.Text = "Invalid password!";
                        txtPWD.Text = "";
                        txtPWD.Focus();
                        return;
                    }
                    else
                    {
                        txtNotice.Text = "";
                        this.DialogResult = System.Windows.Forms.DialogResult.OK;
                        this.Close();
                    }
                }
            });
        }
        void UnityEvents()
        {
            
            proc.ProcessCompleted += new ProcessHandler(delegate(string id, string desc)
            {
                t.SafeCall(this, delegate
                {
                    switch (id)
                    {
                        case "CHECKUSERDATA":
                            LogState(true);
                            proc.DBPUX_Data_Fetch(out dat);
                            if (dat[0].Data_Count == 0)
                            {
                                txtNotice.Text = "Username not found!";
                                LogState(true);
                                txtUID.Text = "";
                                txtUID.Focus();
                                return;
                            }
                            dat[0].Data_Get(0, out user);
                            PWD = user.Data_GetField(2).ToString();
                            txtPWD.Focus();
                            break;
                    }
                });
            });
            proc.ProcessError += new ProcessHandler(delegate(string id, string desc)
            {
                t.SafeCall(this, delegate
                {
                    MessageBox.Show(desc);
                    LogState(true);
                });
            });
            
        }
        void LogState(bool state)
        {
            txtUID.Enabled = state;
            txtPWD.Enabled = state;
        }
        public UnityElement UserDetail
        {
            get { return user; }
        }
    }
}
